package com.nage.components.display;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.util.Log;

import com.nage.components.Component;
import com.nage.engine.GameObject;
import com.nage.graphics.RenderSystem;

public class Shape extends Component implements Drawable {

	// The render component allows an object to be rendered into the game world.
	// It requires an x and a y coordinate which is the position of the bottom left
	// corner
	// of the GameObject. Basic functions are provided for drawing different
	// shapes and
	// setting their colours. If more complex shapes are required, add the
	// Sprite component.

	public float width;
	public float height;
	public float x, y;
	long updateTime, renderTime;
	public float r = 0;
	public float g = 0;
	public float b = 0;
		


	private float tempVertices[] = new float[13];
	private float vertices[] = new float[12];

	// Our vertex buffer.
	private FloatBuffer vertexBuffer;
	ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);



	public Shape(GameObject p) {
		super(p);
		type = SHAPE;
		width = 0.1f;
		height = 0.1f;
		x = parent.getX();
		y = parent.getY();

		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		RenderSystem.registerDrawable(this);
	}

	public void setWidth(String w) {
		if(w!=null) {
			width = Float.parseFloat(w);
		}
	}
	
	public void setHeight(String h) {
		if(h!=null) {
			height = Float.parseFloat(h);
		}
	}
	
	
	public void calcVertices(GL10 gl, Context c) {
		// converts the x, y, z values for each corner of the rectangle from
		// window coords to object coords and puts them into the vertices matrix

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		
		x = parent.getX();
		y = parent.getY();
		
		tempVertices[0] = x*RenderSystem.view[2]; 			tempVertices[1] = y*RenderSystem.view[3]; 			tempVertices[2] = 0; //BL
		tempVertices[3] = (x+width)*RenderSystem.view[2]; 	tempVertices[4] = y*RenderSystem.view[3]; 			tempVertices[5] = 0; //BR
		tempVertices[6] = x*RenderSystem.view[2]; 			tempVertices[7] = (y+height)*RenderSystem.view[3]; 	tempVertices[8] = 0; //TL
		tempVertices[9] = (x+width)*RenderSystem.view[2]; 	tempVertices[10] = (y+height)*RenderSystem.view[3]; tempVertices[11] = 0; //TR

		System.arraycopy(tempVertices, 0, vertices, 0, 12);
		vertexBuffer.clear();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
	}

	@Override
	public void update(long delta) {

	}

	@Override
	public void render(GL10 gl) {
		if(parent.hasMoved) {
			calcVertices(gl, null);
			parent.hasMoved = false;
			//Log.d("Shape", "moved");

		}
		
		gl.glLoadIdentity();
		
		gl.glColor4f(r, g, b, 1.0f);
	//	Log.d("Shape", parent.name+"start rgb = "+r+g+b);
		gl.glTranslatef(0.0f, 0.0f, -1.0f);

		// Set the face rotation
		gl.glFrontFace(GL10.GL_CW);

		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

		// Enable vertex buffer
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		// Draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

		// Disable the client state before leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	//	Log.d("Shape", "finish rgb = "+r+g+b);

	}

	public void setRGB(String rgb) {
		if(rgb!=null) {
			r = ((float)Integer.valueOf(rgb.substring(0, 2), 16).intValue())/255;
			g = ((float)Integer.valueOf(rgb.substring(2, 4), 16).intValue())/255;
			b = ((float)Integer.valueOf(rgb.substring(4, 6), 16).intValue())/255;
		}		
	}

}
